GNU bison, commonly known as Bison, is a parser generator that is part of the GNU Project. Bison reads a specification of a context-free language, warns about any parsing ambiguities, and generates a parser (either in C, C++, or Java) which reads sequences of tokens and decides whether the sequence conforms to the syntax specified by the grammar. Bison by default generates LALR parsers but can also create GLR parsers. In POSIX mode, Bison is compatible with yacc, but also has several improvements over this earlier program. flex, an automatic lexical analyser, is often used with Bison, to tokenise input data and provide Bison with tokens. Bison was originally written by Robert Corbett in 1988. Later, in 1990, Robert Corbett wrote another parser generator named Berkeley Yacc. Bison was made Yacc-compatible by Richard Stallman. Bison is free software and is available under the GNU General Public License, with an exception (discussed below) allowing its generated code to be used without triggering the copyleft requirements of the licence. ==A complete reentrant parser example==
The following example shows how to use Bison and flex to write a simple calculator program (only addition and multiplication) and a program for creating an abstract syntax tree. The next two files provide definition and implementation of the syntax tree functions.
The tokens needed by the Bison parser will be generated using flex.
Since the tokens are provided by flex we must provide the means to communicate between the parser and the lexer.〔(GNU Bison Manual: C Scanners with Bison Parsers )〕 The data type used for communication, ''YYSTYPE'', is set using Bison's ''%union'' declaration. Since in this sample we use the reentrant version of both flex and yacc we are forced to provide parameters for the ''yylex'' function, when called from ''yyparse''.〔 This is done through Bison's ''%lex-param'' and ''%parse-param'' declarations.〔(GNU Bison Manual: Calling Conventions for Pure Parsers )〕
The code needed to obtain the syntax tree using the parser generated by Bison and the scanner generated by flex is the following.